package com.imtimmy.android.pda;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class alarmReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		Log.i("AlarmReceiver", "Called");

		Bundle bundle = intent.getExtras();
		showNotification(context, bundle);

	}

	private void showNotification(Context context, Bundle bundle) {
		int nId = bundle.getInt(DatabaseHelper.COLUMN_ID);
		Long lDate = bundle.getLong(DatabaseHelper.COLUMN_DATE);
		String sTitle = bundle.getString(DatabaseHelper.COLUMN_TITLE);
		String sContent = bundle.getString(DatabaseHelper.COLUMN_CONTENT);

		Notification notification = new Notification(R.drawable.icon, sTitle
				+ "\n" + sContent, lDate);
		Intent intent = new Intent(context, ViewActivity.class);
		intent.putExtras(bundle);
		PendingIntent contentIntent = PendingIntent.getActivity(context, nId,
				intent, 0);
		notification.setLatestEventInfo(context, sTitle, sContent,
				contentIntent);
		notification.flags |= Notification.FLAG_AUTO_CANCEL;
		notification.defaults |= Notification.DEFAULT_ALL;
		NotificationManager notificationManager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		notificationManager.cancel(nId);
		notificationManager.notify(nId, notification);
	}
}
